package fragments;

import helper.GetFBUserPhotoHelper;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.matchcalendar.R;

import dataFormat.FBUser;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class WhoHaveTimeFragment extends Fragment {
	private SparseArray<JSONObject> positionTimeRangeMap;
	private List<FBUser> friendList;
	private FBUser selFriend = null;
	private String selStartTime = null;
	private String selOverTime = null;
	
	public FBUser getSelFriend() {
		return selFriend;
	}
	
	public String getSelStartTime() {
		return selStartTime;
	}
	
	public String getSelOverTime() {
		return selOverTime;
	}
	
	public WhoHaveTimeFragment(SparseArray<JSONObject> positionTimeRangeMap, List<FBUser> friendList) {
		this.positionTimeRangeMap = positionTimeRangeMap;
		this.friendList = friendList;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ListView whoHaveTimeListView = new ListView(getActivity());
		
		whoHaveTimeListView.setAdapter(new WhoHaveTimeAdapter(
				getActivity(), android.R.layout.simple_list_item_1, friendList));
		whoHaveTimeListView.setOnItemClickListener(new WhoHaveTimeListViewItemClick());
		return whoHaveTimeListView;
	}
	
	private class WhoHaveTimeAdapter extends ArrayAdapter<FBUser> {
		private SparseArray<View> findRow = new SparseArray<View>();
		
		public WhoHaveTimeAdapter(Context context, int textViewResourceId, List<FBUser> objects) {
			super(context, textViewResourceId, objects);
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			if (findRow.get(position) == null) {
				View view = View.inflate(getActivity(), R.layout.custom_who_have_time_choose_view, null);
				TextView textViewName = (TextView) view.findViewById(R.id.textViewName);
				
				textViewName.setText(getItem(position).getName());
				findRow.put(position, view);
				new GetFriendPhotos().execute(getItem(position));
				return view;
			}
			else if (getItem(position).getPhoto() == null)
				return findRow.get(position);
			else {
				ImageView imageViewHead = (ImageView) findRow.get(position).findViewById(R.id.imageViewHead);
				Drawable drawableBitmap = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(
						getItem(position).getPhoto(), imageViewHead.getWidth(), imageViewHead.getHeight(), true));
				
				imageViewHead.setImageDrawable(drawableBitmap);
				return findRow.get(position);
			}
		}
		
		private class GetFriendPhotos extends GetFBUserPhotoHelper {

			@Override
			protected void onPostExecute(FBUser fbUser) {
				notifyDataSetChanged();
			}
		}
	}
	
	private class WhoHaveTimeListViewItemClick implements ListView.OnItemClickListener {
		private View previousSelView = null;

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
			try {
				JSONArray timeRangesJsonArr = positionTimeRangeMap.get(position).getJSONArray("timeRanges");
				String[] timeRanges = new String[timeRangesJsonArr.length()];
				
				for (int i=0; i<timeRangesJsonArr.length(); i++) {
					JSONObject timeRangeJsonObj = timeRangesJsonArr.getJSONObject(i);
					
					timeRanges[i] = timeRangeJsonObj.getString("startTime") + "~" +
					timeRangeJsonObj.getString("overTime");
				}
				new AlertDialog.Builder(getActivity())
				.setTitle("請選擇您想揪您朋友的時段")
				.setItems(timeRanges, new WhoHaveTimeDialogItemClick(view, position, timeRangesJsonArr))
				.show();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		private class WhoHaveTimeDialogItemClick implements DialogInterface.OnClickListener {
			private View view;
			private int position;
			private JSONArray timeRangesJsonArr;
			
			public WhoHaveTimeDialogItemClick(View view, int position, JSONArray timeRangesJsonArr) {
				this.view = view;
				this.position = position;
				this.timeRangesJsonArr = timeRangesJsonArr;
			}

			@Override
			public void onClick(DialogInterface dialog, int which) {
				try {
					TextView textViewTime = (TextView) view.findViewById(R.id.textViewTime);
					ImageView imageViewCheck = (ImageView) view.findViewById(R.id.imageViewCheck);
					JSONObject timeRangeJsonObj = timeRangesJsonArr.getJSONObject(which);
					
					if (previousSelView != null) {
						((ImageView)previousSelView.findViewById(R.id.imageViewCheck)).
						setImageResource(R.drawable.com_facebook_button_check_off);
						((TextView)previousSelView.findViewById(R.id.textViewTime)).setVisibility(View.GONE);
					}
					previousSelView = view;
					textViewTime.setText("活動時間: " + timeRangeJsonObj.getString("startTime") + "~" +
					timeRangeJsonObj.getString("overTime"));
					textViewTime.setVisibility(View.VISIBLE);
					imageViewCheck.setImageResource(R.drawable.com_facebook_button_check_on);
					selFriend = friendList.get(position);
					selStartTime = timeRangeJsonObj.getString("startTime");
					selOverTime = timeRangeJsonObj.getString("overTime");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
